<template>
  <el-dialog
    v-model="centerDialogVisible"
    :title="t('dbms.dicConvert.valueTitle')"
    align-center
    width="90%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-container>
      <el-aside width="200px">
        <el-scrollbar wrap-style="calc(100vh - 205px);">
          <el-tree
            ref="treeEl"
            style="max-width: 600px"
            :load="loadNode"
            :props="treeProps"
            :default-expanded-keys="['root']"
            :expand-on-click-node="false"
            node-key="id"
            lazy
            @node-click="clickTreeNode"
          >
            <template #default="{ node }">
              <template v-if="node.data.type == 'Join'">
                <Icon icon="material-symbols:dictionary" color="#00FFFF" hoverColor="#00FFFF" />
              </template>
              <template v-else>
                <Icon icon="material-symbols:menu" color="#00DD00	" hoverColor="#00DD00	" />
              </template>
              <span class="ml-1">{{ node.label }}</span>
            </template>
          </el-tree>
        </el-scrollbar>
      </el-aside>
      <WsmTable
        ref="table"
        v-model="queryFormType"
        load-url="/dbms-server/base/dictionarytype/valuespage"
        :change-load="true"
      >
        <template #title>{{ name }}</template>
        <template #button="{ row }">
          <template v-if="type == 'Join'">
            <el-button v-hasPermi="'initValue'" @click="initValue()" type="success">{{
              t('dbms.dicConvert.initValue')
            }}</el-button>
            <el-button v-hasPermi="'clearValue'" @click="clearValue()" type="warning">{{
              t('dbms.dicConvert.clearValue')
            }}</el-button>
          </template>
          <template v-if="row">
            <el-button v-hasPermi="'update'" @click="update(row.infoId)" type="success">{{
              t('common.update')
            }}</el-button>
          </template>
        </template>
        <template #column>
          <el-table-column prop="infoId" :label="t('dbms.dicConvert.dicInfo.id')" width="200" />
          <el-table-column prop="name" :label="t('dbms.dicConvert.dicInfo.name')" />
          <el-table-column
            prop="infoValue"
            :label="t('dbms.dicConvert.dicInfo.infoValue')"
            width="150"
          />
          <el-table-column
            prop="dicValue"
            :label="t('dbms.dicConvert.dicInfo.dicValue')"
            width="150"
          />
        </template>
      </WsmTable>
    </el-container>

    <WsmForm
      ref="updateForm"
      v-model="updateData"
      load-url="/dbms-server/base/dictionarytype/getValue"
      submit-url="/dbms-server/base/dictionarytype/addvalue"
      :width="600"
      label-width="100px"
      :title="t('dbms.dicConvert.updateValueTitle')"
      @success="flushed"
    >
      <template #default="{ form }">
        <el-row>
          <el-col :span="24">
            <el-form-item prop="value" :label="t('dbms.dicConvert.dicInfo.dicValue')">
              <el-input v-model="form.value" :placeholder="t('dbms.dicConvert.dicInfo.dicValue')" />
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </WsmForm>
  </el-dialog>
</template>
<script setup lang="ts">
import { WsmTable } from '@/components/WsmTable'
import { WsmForm } from '@/components/WsmForm'
import { QueryFormType } from '@/components/WsmTable/src/types'
import { ref } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { FormDataType } from '@/components/WsmForm/src/types'
import { getTreeChildDictionaryInfoApi } from '@/api/dbms/dicmanager'
import { ElMessage, ElMessageBox, ElTree } from 'element-plus'
import type Node from 'element-plus/es/components/tree/src/model/node'
import { clearValueApi, dtjlistApi, initValueApi } from '@/api/dbms/dictionarytype'

const centerDialogVisible = ref<boolean>(false)

const { t } = useI18n()

const typeCode = ref<string>('')

const code = ref<string>('')

const name = ref()

const type = ref()

//表格对象
const table = ref()

//修改表单对象
const updateForm = ref()
const updateData: FormDataType = {
  infoId: '',
  typeCode: '',
  value: ''
}

const queryFormType: QueryFormType = {
  typeCode: {
    value: 'root',
    clear: false
  },
  code: {
    value: 'root',
    clear: false
  },
  parentId: {
    value: 'root',
    clear: false
  }
}
const treeProps = {
  parentId: 'parentId',
  isLeaf: 'isLeaf',
  label: 'name'
}

const treeEl = ref<InstanceType<typeof ElTree>>()

//加载树代码
const loadNode = async (node: Node, resolve: (data) => void) => {
  if (node.level == 0) {
    const rs = await dtjlistApi(typeCode.value)
    if (rs.success) {
      return resolve(rs.data)
    }
  } else if (node.level == 1) {
    const rs = await getTreeChildDictionaryInfoApi({ code: node.data.code, parentId: 'root' })
    if (rs.success) {
      return resolve(rs.data)
    }
  } else {
    const rs = await getTreeChildDictionaryInfoApi({ code: node.data.code, parentId: node.data.id })
    if (rs.success) {
      return resolve(rs.data)
    }
  }
}

//树的点击事件
const clickTreeNode = (data) => {
  initParams(data)
  table.value.load(1)
}

const initParams = (data) => {
  type.value = data.type
  name.value = data.name
  queryFormType.code.value = data.code
  code.value = data.code
  if (data.type == 'Join') {
    queryFormType.parentId.value = 'root'
  } else {
    queryFormType.parentId.value = data.id
  }
}

const update = (infoId: string) => {
  updateForm.value.show({ typeCode: typeCode.value, infoId })
}
const initValue = async () => {
  ElMessageBox.confirm(t('dbms.dicConvert.initMessage'), t('common.delWarning'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  }).then(async () => {
    const rs = await initValueApi({ code: code.value, typeCode: typeCode.value })
    if (rs.success) {
      ElMessage.success(rs.message)
      table.value.load(1)
    }
  })
}

const clearValue = async () => {
  ElMessageBox.confirm(t('dbms.dicConvert.clearMessage'), t('common.delWarning'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  }).then(async () => {
    const rs = await clearValueApi({ code: code.value, typeCode: typeCode.value })
    if (rs.success) {
      table.value.load(1)
    }
  })
}

const flushed = () => {
  table.value.load(1)
}

const show = (c: string) => {
  typeCode.value = c
  queryFormType.typeCode.value = c
  centerDialogVisible.value = true
}

//关闭表单
const hide = () => {
  centerDialogVisible.value = false
}

//暴露方法
defineExpose({ show, hide })
</script>

<style scoped></style>
